package at.technikum_wien.embsys.aat.PriscillaCore.component;

import java.util.Iterator;
import java.util.List;

import at.technikum_wien.embsys.aat.PriscillaCore.enums.EnoceanComponentType;
import at.technikum_wien.embsys.aat.PriscillaCore.enums.ErrorCode;
import at.technikum_wien.embsys.aat.PriscillaCore.event.FrameEventError;
import at.technikum_wien.embsys.aat.PriscillaCore.link.event.LinkListener;
import at.technikum_wien.embsys.aat.PriscillaCore.telegram.Telegram;



/**
 * "Component" for return and error messages
 * 
 * This component is used for error message exchange and as return type
 * for a invalid component
 * 
 * @author R. Wagner
 *
 */
public class ErrorMsg implements EnoceanComponent {
	List<?> eventListener;
	EnoceanComponentType enoceanComponentType = EnoceanComponentType.Error;
	Telegram telegram;
	String errorMsg;
	ErrorCode errorCode;

	public ErrorMsg(Telegram telegram, ErrorCode errorCode, String errorMsg, List<?> listener) {
		super();
		this.telegram = telegram;
		this.errorMsg = errorMsg;
		this.eventListener = listener;
		this.errorCode = errorCode;
		fireFrame();
	}

	public ErrorMsg(Telegram telegram, ErrorCode errorCode, List<?> listener) {
		super();
		this.telegram = telegram;
		this.errorCode = errorCode;
		this.eventListener = listener;
		fireFrame();
	}

	void fireFrame() {
		Iterator<?> listen = eventListener.iterator();
		final FrameEventError frameLearnError = new FrameEventError(this, telegram.getDeviceID(), errorCode, errorMsg);
		while (listen.hasNext()) {
			((LinkListener) listen.next()).frameReceived_Error(frameLearnError);
		}
	}

	@Override
	public String getDeviceID() {
		return telegram.getDeviceID();
	}

	@Override
	public String getORG() {
		return telegram.getxORG();
	}

	@Override
	public EnoceanComponentType getType() {
		return enoceanComponentType;
	}

	@Override
	public void setDeviceID(String deviceID) {
		telegram.setDeviceID(deviceID);
	}

	public String getErrorMsg() {
		return errorMsg;
	}

}
